package com.six.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.bean.Review;
import com.six.bean.Tadmin;
import com.six.service.ReviewService;
import com.six.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("review")
@CrossOrigin
public class ReviewController {
    @Autowired
    ReviewService reviewService;

    @GetMapping("list")
    public Result getList() {
        List<Review> list = reviewService.list();
        return Result.success(list);
    }

    @PostMapping
    public Result add(@RequestBody Review review) {
        reviewService.save(review);
        Review review1 = reviewService.getById(review.getRid());
        return Result.success("保存成功", review1);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int rid) {
        boolean b = reviewService.removeById(rid);
        if (b) {
            return Result.success("删除成功");
        } else {
            return Result.fail(201, "删除失败");
        }
    }

    @PutMapping
    public Result edit(@RequestBody Review review) {
        boolean b = reviewService.saveOrUpdate(review);
        Review review1 = reviewService.getById(review.getRid());
        if (b) {
            return Result.success("保存成功", review1);
        } else {
            return Result.fail(201, "修改失败");
        }
    }

    @GetMapping("page")
    public Result getPage(Page page, @RequestParam("searchtext") String searchtext) {
        System.out.println(searchtext);
        IPage<Tadmin> result = null;
        if (searchtext == null || searchtext.trim().isEmpty()) {
            result = reviewService.page(page);
        } else {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.like("content", searchtext);

            result = reviewService.page(page, wrapper);
        }

        return Result.success(result);
    }
}

